﻿using ScrollerMusicPlayerUI.Controls.Primitives;
using System.Windows;
using System.Windows.Controls;

namespace ScrollerMusicPlayerUI.Controls
{
    public class BetterWarpPanel : Panel
    {
        #region dp props

        ///<summary>列的数量。</summary>
        public int Columns { get { return (int)GetValue(ColumnsProperty); } set { SetValue(ColumnsProperty, value); } }
        public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(int), typeof(BetterWarpPanel), new PropertyMetadata(5));

        ///<summary>可平铺的成员的原始列表</summary>
        public BlockInfo[] BlockInfos { get { return (BlockInfo[])GetValue(BlockInfosProperty); } set { SetValue(BlockInfosProperty, value); } }
        public static readonly DependencyProperty BlockInfosProperty = DependencyProperty.Register("BlockInfos", typeof(BlockInfo[]), typeof(BetterWarpPanel), new PropertyMetadata(null));

        public double Offset { get { return (double)GetValue(OffsetProperty); } set { SetValue(OffsetProperty, value); } }
        public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register("Offset", typeof(double), typeof(BetterWarpPanel),
            new FrameworkPropertyMetadata(0d)
            {
                AffectsArrange = true
            });

        #endregion

        #region fields

        private FillManager _fillManager;
        private ScrollerManager _scrollerManager;

        #endregion

        #region ctors

        public BetterWarpPanel()
        {
            SizeChanged += BetterWarpPanel_SizeChanged;
            Loaded += BetterWarpPanel_Loaded;
        }



        #endregion

        #region overrides

        protected override Size ArrangeOverride(Size finalSize)
        {
            var size = finalSize.Width / Columns;

            for (int i = 0; i < Children.Count; i++)
            {
                var item = Children[i];
                var rid = i / Columns;
                var cid = i % Columns;
                var left = cid * size;
                var top = rid * size + Offset;
                item.Arrange(new Rect(left, top, size, size));
                if (item is FrameworkElement fwElem)
                {
                    fwElem.Width = size; fwElem.Height = size;
                }
            }
            return base.ArrangeOverride(finalSize);
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
           
            base.OnPropertyChanged(e);
        }

        #endregion

        #region methods

        private void Fill()
        {
            if (_fillManager == null) return;

            _fillManager.Fill();
        }

        private void BetterWarpPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (_fillManager == null) return;
            _fillManager.ContinueFill();
        }

        private void BetterWarpPanel_Loaded(object sender, RoutedEventArgs e)
        {
            _fillManager = new FillManager(this, BlockInfos);
            _scrollerManager = new ScrollerManager(this, _fillManager);
            _scrollerManager.Start();

            Fill();
        }

        #endregion
    }
}
